package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.WdyUser;
import com.ruoyi.system.service.IWdyUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-03-14
 */
@Controller
@RequestMapping("/system/wdyUser")
public class WdyUserController extends BaseController
{
	private Logger logger = LoggerFactory.getLogger(WdyUserController.class);
    private String prefix = "system/wdyUser";

	@Autowired
	private IWdyUserService wdyUserService;

	@RequiresPermissions("system:wdyUser:view")
	@GetMapping()
	public String wdyUser()
	{
	    return prefix + "/wdyUser";
	}
	
	/**
	 * 查询用户列表
	 */
	@RequiresPermissions("system:wdyUser:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(WdyUser wdyUser)
	{
		startPage();
        List<WdyUser> list = wdyUserService.selectWdyUserList(wdyUser);
		for(WdyUser e : list) {
			QueryWrapper<WdyUser> wdyUserQueryWrapper = new QueryWrapper<>();
			wdyUserQueryWrapper.eq("user_id", e.getReferrerId());
			WdyUser referrer = wdyUserService.getOne(wdyUserQueryWrapper);
			if(referrer != null) {
				e.addExtraData("referrerMobile", referrer.getUserName());
			}
			wdyUserQueryWrapper = new QueryWrapper<>();
			wdyUserQueryWrapper.eq("user_id", e.getLeaderId());
			WdyUser leader = wdyUserService.getOne(wdyUserQueryWrapper);
			if(leader != null) {
				e.addExtraData("leaderMobile", leader.getUserName());
			}
		}
		return getDataTable(list);
	}
	
	
	/**
	 * 导出用户列表
	 */
	@RequiresPermissions("system:wdyUser:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WdyUser wdyUser)
    {
    	List<WdyUser> list = wdyUserService.selectWdyUserList(wdyUser);
        ExcelUtil<WdyUser> util = new ExcelUtil<WdyUser>(WdyUser.class);
        return util.exportExcel(list, "wdyUser");
    }
	
	/**
	 * 新增用户
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存用户
	 */
	@RequiresPermissions("system:wdyUser:add")
	@Log(title = "用户", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(WdyUser wdyUser)
	{
		try {
			wdyUserService.register(wdyUser);
		} catch (Exception e) {
			log.error("添加异常", e);
			return error();
		}
		return success();
	}

	/**
	 * 修改用户
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		WdyUser wdyUser = wdyUserService.selectWdyUserById(id);
		mmap.put("wdyUser", wdyUser);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存用户
	 */
	@RequiresPermissions("system:wdyUser:edit")
	@Log(title = "用户", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(WdyUser wdyUser)
	{		
		return toAjax(wdyUserService.updateWdyUser(wdyUser));
	}
	
	/**
	 * 删除用户
	 */
	@RequiresPermissions("system:wdyUser:remove")
	@Log(title = "用户", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(wdyUserService.deleteWdyUserByIds(ids));
	}

	@RequiresPermissions("system:wdyUser:view")
	@GetMapping("/referrer")
	public String getReferrer(String userId, ModelMap mmap)
	{
		String startTime = DateUtils.getMonthFirstDayStr(0);
		String endTime = DateUtils.getMonthLastDayStr(0);
		mmap.put("startTime", startTime);
		mmap.put("endTime", endTime);
		mmap.put("userId", userId);
		return prefix + "/referrer";
	}

	@RequiresPermissions("system:wdyUser:view")
	@PostMapping("/referrer")
	@ResponseBody
	public AjaxResult postReferrer(String userId, String startTime, String endTime, ModelMap mmap)
	{
		if(StringUtils.isEmpty(startTime) && StringUtils.isEmpty(endTime)) {
			startTime = DateUtils.getMonthFirstDayStr(0);
			endTime = DateUtils.getMonthLastDayStr(0);
		}
		Map<String, Map<String, Object>> teamMap = new HashMap<>();
		Map<String, Object> data = wdyUserService.getReferrerStatistic(userId, startTime, endTime, teamMap);
		return AjaxResult.success().put("data", data);
	}

}
